--This Library allowss CoDE to override the Gcombat and CDS damage functions with its own

function CDamage_PerformOverride()
	MsgN("Contraption Damage Engine: Commencing Override Preload")
	--Store original Gcombat functions elsewhere
	if (COMBATDAMAGEENGINE) then
		CDE_Gcombat_Default_Direct = gcombat.devhit
		CDE_Gcombat_Default_Area = gcombat.hcgexplode
		MsgN("Contraption Damage Engine: GCombat Detected")
	else
		MsgN("Contraption Damage Engine: GCombat NOT Detected")
	end
	
	--Store original CDS functions elsewhere
	if (CombatDamageSystem) then
		CDE_CDS_Default_DamageEnt = cds_damageent
		CDE_CDS_Default_DamagePos = cds_damagepos
		CDE_CDS_Default_Explosion = cds_explosion
		MsgN("Contraption Damage Engine: CDS Detected")
	else
		MsgN("Contraption Damage Engine: CDS NOT Detected")
	end
	
	
	Msg("Contraption Damage Engine: Commencing CDS/Gcombat Function Override\n")
	
	
	--Override Gcombat functions with our new handlers
	gcombat.devhit = CDamage_Handler_GCombat_Direct
	gcombat.hcgexplode = CDamage_Handler_GCombat_Area
	cbt_dealdevhit = CDamage_Handler_GCombat_Direct
	cbt_hcgexplode = CDamage_Handler_GCombat_Area
	
	--Override CDS functions with our new handlers
	cds_damageent = CDamage_Handler_CDS_Direct
	cds_damagepos = CDamage_Handler_CDS_Position
	cds_explosion = CDamage_Handler_CDS_Explosion
	
	Msg("Contraption Damage Engine: Function Override Completed\n")
end

hook.Add("InitPostEntity", "CDamage_Override", CDamage_PerformOverride)

function CDamage_Handler_GCombat_Direct(entity, damage, pierce)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideGcombat"))) or (!COMBATDAMAGEENGINE) then
		CDamage_damageent(entity, damage, nil)
		return 0
	else
		CDE_Gcombat_Default_Direct(entity, damage, pierce)
	end
end

function CDamage_Handler_GCombat_Area(position, radius, damage, pierce)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideGcombat"))) or (!COMBATDAMAGEENGINE) then
		CDamage_blastdamage(position, radius, damage, nil, false)
		return 0
	else
		CDE_Gcombat_Default_Area(position, radius, damage, pierce)
	end
end

function CDamage_Handler_CDS_Direct(ent, damage, pierce, inflictor, heat, gcombat)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideCDS"))) or (!CombatDamageSystem) then
		CDamage_damageent(ent, damage, inflictor)
	else
		CDE_CDS_Default_DamageEnt(ent, damage, pierce, inflictor, heat, gcombat)
	end
end

function CDamage_Handler_CDS_Position(pos, damage, pierce, radius, inflictor)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideCDS"))) or (!CombatDamageSystem) then
		CDamage_blastdamage(pos, radius, damage, inflictor, false)
	else
		CDE_CDS_Default_DamagePos(pos, damage, pierce, radius, inflictor)
	end
end

function CDamage_Handler_CDS_Explosion(pos, radius, damage, pierce, defeffect, inflictor)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideCDS"))) or (!CombatDamageSystem) then
		CDamage_blastdamage(pos, radius, damage, inflictor, false)
	else
		CDE_CDS_Default_Explosion(pos, radius, damage, pierce, defeffect, inflictor)
	end
end